//
//  SearchHomeViewController.m
//  TravelFollowYourHeart
//
//  Created by 鲁万玺 on 2017/12/13.
//  Copyright © 2017年 Lwx. All rights reserved.
//

#import "SearchHomeViewController.h"
#import "PageCollectionView.h"
#import "HomeCollectionViewCell.h"
#import "TravelInforViewController.h"
@interface SearchHomeViewController ()<UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource, UIScrollViewDelegate>

@property (nonatomic, strong)UISearchBar *searchBar;
@property (nonatomic, strong)UIView *lineView;
@property (nonatomic, strong)PageCollectionView * collectionView;
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UIButton *btn3;
@property (weak, nonatomic) IBOutlet UIButton *btn4;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIView *titleHomeView;

@property (nonatomic, strong)UIScrollView *scrollView;

@end

@implementation SearchHomeViewController(Networking)
- (void)searchTableView:(PageCollectionView *)collectionView
{
    NSDictionary *dic = @{
                          @"rule":collectionView.tagId,
                          @"row":@(10),
                          @"lat":@([LocationTool shareIntance].latitude),
                          @"lng":@([LocationTool shareIntance].longitude),
                          @"page":@(collectionView.page),
                          @"keyword":self.searchBar.text
                          };
    
    [[HttpRequest shardWebUtil]postNetworkRequestURLString:Url(@"/index/travels") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        [collectionView.mj_footer endRefreshing];
        [collectionView.mj_header endRefreshing];
        if (error)
        {
        }
        else
        {
            if ([obj[@"code"] integerValue] == 100000)
            {
                HomeRMTJModel *model = [HomeRMTJModel yy_modelWithDictionary:obj];
                if (collectionView.page == 1)
                {
                    [collectionView.tableDataArr removeAllObjects];
                }
                [collectionView.tableDataArr addObjectsFromArray:model.data];
                [collectionView reloadData];
                
            }
        }

    }];
}

@end

@implementation SearchHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavUI];
    [self setUI];
}

- (void)setNavUI
{
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 220/WIDTH_5S_SCALE, 30)];
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,220/WIDTH_5S_SCALE,30)];
    [self.searchBar setCornerRadius:15];
    self.searchBar.barStyle = UIBarStyleDefault;
    self.searchBar.placeholder = @"搜索";
    self.searchBar.backgroundImage = [UIColor imageWithColor: [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1]];
    self.searchBar.delegate = self;
    UITextField *searchField = [self.searchBar valueForKey:@"searchField"];
    if (searchField)
    {
        [searchField setBackgroundColor:[UIColor clearColor]];
        searchField.textColor = [UIColor darkGrayColor];
    }
    [titleView addSubview:self.searchBar];
    self.navigationItem.titleView = titleView;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
//    [self searchTableView:self.collectionView];
    
    
    for (id view in self.scrollView.subviews)
    {
        if ([view isKindOfClass:[UICollectionView class]])
        {
            PageCollectionView *collection = (PageCollectionView *)view;
            [self searchTableView:collection];
        }
    }
    
    
}

- (void)setUI
{
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = [UIColor themeColor];
    self.lineView.frame = CGRectMake(10, 60-8, (SCREEN_WIDTH-20)/4 -3, 3);
    [self.titleHomeView addSubview:self.lineView];
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.bgView.height)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.scrollEnabled = NO;
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH*4, 0);
    self.scrollView.delegate = self;
    [self.bgView addSubview:self.scrollView];
    
    for (int i = 0; i<4; i++)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(SCREEN_WIDTH/2 - 0.5, 200/WIDTH_5S_SCALE);
        layout.minimumLineSpacing = 0.5;
        layout.minimumInteritemSpacing = 0.5;
        
        self.collectionView = [[PageCollectionView alloc]initWithFrame:CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT -64 -60) collectionViewLayout:layout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1];
        self.collectionView.showsVerticalScrollIndicator = NO;
        self.collectionView.tagId = [NSString stringWithFormat:@"%d",i+1];
        
        WS(ws)
        __weak __typeof(&*self.collectionView)weakCollection = self.collectionView;
        [ws.collectionView setMJHeadRefresh:^{
//            [ws getTableView:weakCollection];
            [ws searchTableView:weakCollection];
        }];
        [self.collectionView setMJFootRefresh:^{
//            [ws getTableView:weakCollection];
            [ws searchTableView:weakCollection];
        }];
        
        [self.scrollView addSubview:self.collectionView];
        
        [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([HomeCollectionViewCell class]) ];
    }
}

#pragma mark -UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    PageCollectionView *collection = (PageCollectionView *)collectionView;
    return collection.tableDataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeCollectionViewCell class]) forIndexPath:indexPath];
    
    PageCollectionView *collection = (PageCollectionView *)collectionView;
    cell.modelData = collection.tableDataArr[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([UserModel isSaveLogin]) {
        TravelInforViewController *vc = [[TravelInforViewController alloc]init];
        HomeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeCollectionViewCell class]) forIndexPath:indexPath];
        PageCollectionView *collection = (PageCollectionView *)collectionView;
        cell.modelData = collection.tableDataArr[indexPath.row];
        vc.tid = [cell.modelData.dataId intValue];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        [AppHelper showLogin];
    }
}

#pragma mark - Event
- (IBAction)chooseList:(UIButton *)sender
{
    if (sender.selected) {
        return;
    }
    
    NSInteger i = 0;
    if (sender == self.btn1)
    {
        i = 0;
    }
    else if (sender == self.btn2)
    {
        i = 1;
    }
    else if (sender == self.btn3)
    {
        i = 2;
    }
    else
    {
        i = 3;
    }
    
    self.btn1.selected = NO;
    self.btn2.selected = NO;
    self.btn3.selected = NO;
    self.btn4.selected = NO;
    
    sender.selected = YES;
    WS(ws)
    
    [UIView animateWithDuration:0.5 animations:^{
        ws.lineView.x = (SCREEN_WIDTH-20)/4 *i +10;
        [ws.scrollView setContentOffset:CGPointMake(i*SCREEN_WIDTH, 0) animated:YES];
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.searchBar resignFirstResponder];
}

@end
